package org.dromara.common.web.core;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.LocaleResolver;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.util.Locale;

/**
 * 获取请求头国际化信息
 *
 * @author Lion Li
 */
@Slf4j
public class I18nLocaleResolver implements LocaleResolver {

    @Override
    public Locale resolveLocale(HttpServletRequest httpServletRequest) {
        String language = httpServletRequest.getHeader("Content-Language");
        Locale locale = Locale.getDefault();
        if (language != null && !language.isEmpty()) {
            if (language.contains("_")) {
                String[] split = language.split("_");
                locale = new Locale(split[0], split[1]);
            } else {
                locale = new Locale(language);
            }
        }
        return locale;
    }

    @Override
    public void setLocale(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Locale locale) {

    }
}
